home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / cmd.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  17KB  |  380 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.cmd
  5.  
  6. Provides the Command class, the base class for the command classes
  7. in the distutils.command package.
  8. '''
  9. __revision__ = '$Id: cmd.py 37828 2004-11-10 22:23:15Z loewis $'
  10. import sys
  11. import os
  12. import string
  13. import re
  14. from types import *
  15. from distutils.errors import *
  16. from distutils import util, dir_util, file_util, archive_util, dep_util
  17. from distutils import log
  18.  
  19. class Command:
  20.     '''Abstract base class for defining command classes, the "worker bees"
  21.     of the Distutils.  A useful analogy for command classes is to think of
  22.     them as subroutines with local variables called "options".  The options
  23.     are "declared" in \'initialize_options()\' and "defined" (given their
  24.     final values, aka "finalized") in \'finalize_options()\', both of which
  25.     must be defined by every command class.  The distinction between the
  26.     two is necessary because option values might come from the outside
  27.     world (command line, config file, ...), and any options dependent on
  28.     other options must be computed *after* these outside influences have
  29.     been processed -- hence \'finalize_options()\'.  The "body" of the
  30.     subroutine, where it does all its work based on the values of its
  31.     options, is the \'run()\' method, which must also be implemented by every
  32.     command class.
  33.     '''
  34.     sub_commands = []
  35.     
  36.     def __init__(self, dist):
  37.         """Create and initialize a new Command object.  Most importantly,
  38.         invokes the 'initialize_options()' method, which is the real
  39.         initializer and depends on the actual command being
  40.         instantiated.
  41.         """
  42.         Distribution = Distribution
  43.         import distutils.dist
  44.         if not isinstance(dist, Distribution):
  45.             raise TypeError, 'dist must be a Distribution instance'
  46.         isinstance(dist, Distribution)
  47.         if self.__class__ is Command:
  48.             raise RuntimeError, 'Command is an abstract class'
  49.         self.__class__ is Command
  50.         self.distribution = dist
  51.         self.initialize_options()
  52.         self._dry_run = None
  53.         self.verbose = dist.verbose
  54.         self.force = None
  55.         self.help = 0
  56.         self.finalized = 0
  57.  
  58.     
  59.     def __getattr__(self, attr):
  60.         if attr == 'dry_run':
  61.             myval = getattr(self, '_' + attr)
  62.             if myval is None:
  63.                 return getattr(self.distribution, attr)
  64.             return myval
  65.         attr == 'dry_run'
  66.         raise AttributeError, attr
  67.  
  68.     
  69.     def ensure_finalized(self):
  70.         if not self.finalized:
  71.             self.finalize_options()
  72.         
  73.         self.finalized = 1
  74.  
  75.     
  76.     def initialize_options(self):
  77.         '''Set default values for all the options that this command
  78.         supports.  Note that these defaults may be overridden by other
  79.         commands, by the setup script, by config files, or by the
  80.         command-line.  Thus, this is not the place to code dependencies
  81.         between options; generally, \'initialize_options()\' implementations
  82.         are just a bunch of "self.foo = None" assignments.
  83.  
  84.         This method must be implemented by all command classes.
  85.         '''
  86.         raise RuntimeError, 'abstract method -- subclass %s must override' % self.__class__
  87.  
  88.     
  89.     def finalize_options(self):
  90.         """Set final values for all the options that this command supports.
  91.         This is always called as late as possible, ie.  after any option
  92.         assignments from the command-line or from other commands have been
  93.         done.  Thus, this is the place to code option dependencies: if
  94.         'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
  95.         long as 'foo' still has the same value it was assigned in
  96.         'initialize_options()'.
  97.  
  98.         This method must be implemented by all command classes.
  99.         """
  100.         raise RuntimeError, 'abstract method -- subclass %s must override' % self.__class__
  101.  
  102.     
  103.     def dump_options(self, header = None, indent = ''):
  104.         longopt_xlate = longopt_xlate
  105.         import distutils.fancy_getopt
  106.         if header is None:
  107.             header = "command options for '%s':" % self.get_command_name()
  108.         
  109.         print indent + header
  110.         indent = indent + '  '
  111.         for option, _, _ in self.user_options:
  112.             option = string.translate(option, longopt_xlate)
  113.             if option[-1] == '=':
  114.                 option = option[:-1]
  115.             
  116.             value = getattr(self, option)
  117.             print indent + '%s = %s' % (option, value)
  118.         
  119.  
  120.     
  121.     def run(self):
  122.         """A command's raison d'etre: carry out the action it exists to
  123.         perform, controlled by the options initialized in
  124.         'initialize_options()', customized by other commands, the setup
  125.         script, the command-line, and config files, and finalized in
  126.         'finalize_options()'.  All terminal output and filesystem
  127.         interaction should be done by 'run()'.
  128.  
  129.         This method must be implemented by all command classes.
  130.         """
  131.         raise RuntimeError, 'abstract method -- subclass %s must override' % self.__class__
  132.  
  133.     
  134.     def announce(self, msg, level = 1):
  135.         """If the current verbosity level is of greater than or equal to
  136.         'level' print 'msg' to stdout.
  137.         """
  138.         log.log(level, msg)
  139.  
  140.     
  141.     def debug_print(self, msg):
  142.         """Print 'msg' to stdout if the global DEBUG (taken from the
  143.         DISTUTILS_DEBUG environment variable) flag is true.
  144.         """
  145.         DEBUG = DEBUG
  146.         import distutils.debug
  147.         if DEBUG:
  148.             print msg
  149.             sys.stdout.flush()
  150.         
  151.  
  152.     
  153.     def _ensure_stringlike(self, option, what, default = None):
  154.         val = getattr(self, option)
  155.         if val is None:
  156.             setattr(self, option, default)
  157.             return default
  158.         if type(val) is not StringType:
  159.             raise DistutilsOptionError, "'%s' must be a %s (got `%s`)" % (option, what, val)
  160.         type(val) is not StringType
  161.         return val
  162.  
  163.     
  164.     def ensure_string(self, option, default = None):
  165.         """Ensure that 'option' is a string; if not defined, set it to
  166.         'default'.
  167.         """
  168.         self._ensure_stringlike(option, 'string', default)
  169.  
  170.     
  171.     def ensure_string_list(self, option):
  172.         '''Ensure that \'option\' is a list of strings.  If \'option\' is
  173.         currently a string, we split it either on /,\\s*/ or /\\s+/, so
  174.         "foo bar baz", "foo,bar,baz", and "foo,   bar baz" all become
  175.         ["foo", "bar", "baz"].
  176.         '''
  177.         val = getattr(self, option)
  178.         if val is None:
  179.             return None
  180.         if type(val) is StringType:
  181.             setattr(self, option, re.split(',\\s*|\\s+', val))
  182.         elif type(val) is ListType:
  183.             types = map(type, val)
  184.             ok = types == [
  185.                 StringType] * len(val)
  186.         else:
  187.             ok = 0
  188.         if not ok:
  189.             raise DistutilsOptionError, "'%s' must be a list of strings (got %r)" % (option, val)
  190.         ok
  191.  
  192.     
  193.     def _ensure_tested_string(self, option, tester, what, error_fmt, default = None):
  194.         val = self._ensure_stringlike(option, what, default)
  195.         if val is not None and not tester(val):
  196.             raise DistutilsOptionError, ("error in '%s' option: " + error_fmt) % (option, val)
  197.         not tester(val)
  198.  
  199.     
  200.     def ensure_filename(self, option):
  201.         """Ensure that 'option' is the name of an existing file."""
  202.         self._ensure_tested_string(option, os.path.isfile, 'filename', "'%s' does not exist or is not a file")
  203.  
  204.     
  205.     def ensure_dirname(self, option):
  206.         self._ensure_tested_string(option, os.path.isdir, 'directory name', "'%s' does not exist or is not a directory")
  207.  
  208.     
  209.     def get_command_name(self):
  210.         if hasattr(self, 'command_name'):
  211.             return self.command_name
  212.         return self.__class__.__name__
  213.  
  214.     
  215.     def set_undefined_options(self, src_cmd, *option_pairs):
  216.         '''Set the values of any "undefined" options from corresponding
  217.         option values in some other command object.  "Undefined" here means
  218.         "is None", which is the convention used to indicate that an option
  219.         has not been changed between \'initialize_options()\' and
  220.         \'finalize_options()\'.  Usually called from \'finalize_options()\' for
  221.         options that depend on some other command rather than another
  222.         option of the same command.  \'src_cmd\' is the other command from
  223.         which option values will be taken (a command object will be created
  224.         for it if necessary); the remaining arguments are
  225.         \'(src_option,dst_option)\' tuples which mean "take the value of
  226.         \'src_option\' in the \'src_cmd\' command object, and copy it to
  227.         \'dst_option\' in the current command object".
  228.         '''
  229.         src_cmd_obj = self.distribution.get_command_obj(src_cmd)
  230.         src_cmd_obj.ensure_finalized()
  231.         for src_option, dst_option in option_pairs:
  232.             if getattr(self, dst_option) is None:
  233.                 setattr(self, dst_option, getattr(src_cmd_obj, src_option))
  234.                 continue
  235.         
  236.  
  237.     
  238.     def get_finalized_command(self, command, create = 1):
  239.         """Wrapper around Distribution's 'get_command_obj()' method: find
  240.         (create if necessary and 'create' is true) the command object for
  241.         'command', call its 'ensure_finalized()' method, and return the
  242.         finalized command object.
  243.         """
  244.         cmd_obj = self.distribution.get_command_obj(command, create)
  245.         cmd_obj.ensure_finalized()
  246.         return cmd_obj
  247.  
  248.     
  249.     def reinitialize_command(self, command, reinit_subcommands = 0):
  250.         return self.distribution.reinitialize_command(command, reinit_subcommands)
  251.  
  252.     
  253.     def run_command(self, command):
  254.         """Run some other command: uses the 'run_command()' method of
  255.         Distribution, which creates and finalizes the command object if
  256.         necessary and then invokes its 'run()' method.
  257.         """
  258.         self.distribution.run_command(command)
  259.  
  260.     
  261.     def get_sub_commands(self):
  262.         """Determine the sub-commands that are relevant in the current
  263.         distribution (ie., that need to be run).  This is based on the
  264.         'sub_commands' class attribute: each tuple in that list may include
  265.         a method that we call to determine if the subcommand needs to be
  266.         run for the current distribution.  Return a list of command names.
  267.         """
  268.         commands = []
  269.         for cmd_name, method in self.sub_commands:
  270.             if method is None or method(self):
  271.                 commands.append(cmd_name)
  272.                 continue
  273.         
  274.         return commands
  275.  
  276.     
  277.     def warn(self, msg):
  278.         sys.stderr.write('warning: %s: %s\n' % (self.get_command_name(), msg))
  279.  
  280.     
  281.     def execute(self, func, args, msg = None, level = 1):
  282.         util.execute(func, args, msg, dry_run = self.dry_run)
  283.  
  284.     
  285.     def mkpath(self, name, mode = 511):
  286.         dir_util.mkpath(name, mode, dry_run = self.dry_run)
  287.  
  288.     
  289.     def copy_file(self, infile, outfile, preserve_mode = 1, preserve_times = 1, link = None, level = 1):
  290.         """Copy a file respecting verbose, dry-run and force flags.  (The
  291.         former two default to whatever is in the Distribution object, and
  292.         the latter defaults to false for commands that don't define it.)"""
  293.         return file_util.copy_file(infile, outfile, preserve_mode, preserve_times, not (self.force), link, dry_run = self.dry_run)
  294.  
  295.     
  296.     def copy_tree(self, infile, outfile, preserve_mode = 1, preserve_times = 1, preserve_symlinks = 0, level = 1):
  297.         '''Copy an entire directory tree respecting verbose, dry-run,
  298.         and force flags.
  299.         '''
  300.         return dir_util.copy_tree(infile, outfile, preserve_mode, preserve_times, preserve_symlinks, not (self.force), dry_run = self.dry_run)
  301.  
  302.     
  303.     def move_file(self, src, dst, level = 1):
  304.         '''Move a file respectin dry-run flag.'''
  305.         return file_util.move_file(src, dst, dry_run = self.dry_run)
  306.  
  307.     
  308.     def spawn(self, cmd, search_path = 1, level = 1):
  309.         '''Spawn an external command respecting dry-run flag.'''
  310.         spawn = spawn
  311.         import distutils.spawn
  312.         spawn(cmd, search_path, dry_run = self.dry_run)
  313.  
  314.     
  315.     def make_archive(self, base_name, format, root_dir = None, base_dir = None):
  316.         return archive_util.make_archive(base_name, format, root_dir, base_dir, dry_run = self.dry_run)
  317.  
  318.     
  319.     def make_file(self, infiles, outfile, func, args, exec_msg = None, skip_msg = None, level = 1):
  320.         """Special case of 'execute()' for operations that process one or
  321.         more input files and generate one output file.  Works just like
  322.         'execute()', except the operation is skipped and a different
  323.         message printed if 'outfile' already exists and is newer than all
  324.         files listed in 'infiles'.  If the command defined 'self.force',
  325.         and it is true, then the command is unconditionally run -- does no
  326.         timestamp checks.
  327.         """
  328.         if exec_msg is None:
  329.             exec_msg = 'generating %s from %s' % (outfile, string.join(infiles, ', '))
  330.         
  331.         if skip_msg is None:
  332.             skip_msg = 'skipping %s (inputs unchanged)' % outfile
  333.         
  334.         if type(infiles) is StringType:
  335.             infiles = (infiles,)
  336.         elif type(infiles) not in (ListType, TupleType):
  337.             raise TypeError, "'infiles' must be a string, or a list or tuple of strings"
  338.         
  339.         if self.force or dep_util.newer_group(infiles, outfile):
  340.             self.execute(func, args, exec_msg, level)
  341.         else:
  342.             log.debug(skip_msg)
  343.  
  344.  
  345.  
  346. class install_misc(Command):
  347.     '''Common base class for installing some files in a subdirectory.
  348.     Currently used by install_data and install_scripts.
  349.     '''
  350.     user_options = [
  351.         ('install-dir=', 'd', 'directory to install the files to')]
  352.     
  353.     def initialize_options(self):
  354.         self.install_dir = None
  355.         self.outfiles = []
  356.  
  357.     
  358.     def _install_dir_from(self, dirname):
  359.         self.set_undefined_options('install', (dirname, 'install_dir'))
  360.  
  361.     
  362.     def _copy_files(self, filelist):
  363.         self.outfiles = []
  364.         if not filelist:
  365.             return None
  366.         self.mkpath(self.install_dir)
  367.         for f in filelist:
  368.             self.copy_file(f, self.install_dir)
  369.             self.outfiles.append(os.path.join(self.install_dir, f))
  370.         
  371.  
  372.     
  373.     def get_outputs(self):
  374.         return self.outfiles
  375.  
  376.  
  377. if __name__ == '__main__':
  378.     print 'ok'
  379.  
  380.